package com.golddaymicromall.activity.jifenconfirmpay;

import android.support.annotation.NonNull;

import com.golddaymicromall.util.Log;
import com.golddaymicromall.util.MD5;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.Map;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.subscriptions.CompositeSubscription;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by lijing on 2016/10/15.
 */

public class BonusPointsConfirmPayPresenter implements BonusPointsConfirmPayContract.Presenter {
    private BonusPointsConfirmPayContract.View mView;

    @NonNull
    private CompositeSubscription mSubscriptions;

    public BonusPointsConfirmPayPresenter(BonusPointsConfirmPayContract.View view) {
        mSubscriptions = new CompositeSubscription();
        mView = checkNotNull(view, "View is not null");
        mView.setPresenter(this);
    }

    @Override
    public void getPanicBuyingAdressConfirmPayInfo(String id) {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.address.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerId", id);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        Subscription subscription = OkGo.get(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .doOnSubscribe(() -> {
                    mView.showLoading();
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    mView.dismissLoading();
                    s = checkNotNull(s, "");
                    mView.showPanicBuyingAdressConfirmPayInfo(s);
                }, throwable -> {
                    throwable.printStackTrace();
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void getPanicBuyingConfirmPayCreateOrderInfo(String ids, String count, String addressid, String customerid, String orderPrice ,String pv,String allpoints,String mDeptFlag) {



        Log.e("idsaa",ids);
        Log.e("countaa",count);
        MD5 md = new MD5();
        String md5Realpayment = md.toMD5(String.valueOf(orderPrice));
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.order.createorder");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("ids", ids);       //产品id
        form.put("allPrice", count); //件数
        form.put("adressid", addressid); //地址
        form.put("customerid", customerid); //客户id
        form.put("orderType", "3000");
        form.put("orderClass", "3000");
        form.put("orderPrice", String.valueOf(orderPrice)); //总价
        form.put("orderRealpayment", String.valueOf(orderPrice)); //实付价
        form.put("orderAllpv", String.valueOf(0)); //pv
        form.put("point", String.valueOf(allpoints));
        form.put("orderRealpv", String.valueOf(0)); //pv
        form.put("md5Realpayment", md5Realpayment);//加密
        form.put("flag", mDeptFlag);
        form.put("orderName", "A");
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        Subscription subscription = OkGo.get(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .doOnSubscribe(() -> {
                    mView.showLoading();
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    mView.dismissLoading();
                    Log.e("showPanicBuyingConfirmPay", s);
                    mView.showPanicBuyingConfirmPayCreateOrderInfo(s);
                }, throwable -> {
                    mView.showError();
                    throwable.printStackTrace();
                });
        mSubscriptions.add(subscription);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        //防止Rx内存泄漏，清空subscribe
        mSubscriptions.clear();
    }


}
